﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace FrameDisplayServer.Browser
{

    public class BrowserManager : IBrowserManager, IDisposable
    {
        private ImageDisplayForm imageForm = null;
        private BrowserForm browserForm = null;


        public void NavigateAndShow(string url)
        {
            if (AppCentral.MainForm.InvokeRequired)
            {
                AppCentral.MainForm.BeginInvoke(new Action<string>(this.NavigateAndShow), new object[] { url });
                return;
            }

            this.EnsureCreatedAndShow();
            this.browserForm.Navigate(url);
        }


        public void ShowImage(Image image)
        {
            if (AppCentral.MainForm.InvokeRequired)
            {
                AppCentral.MainForm.BeginInvoke(new Action<Image>(this.ShowImage), new object[] { image });
                return;
            }

            this.EnsureCreatedAndShowImageForm();
            this.imageForm.ShowImage(image);
        }

        public void HideFrame()
        {
            if (this.imageForm != null)
            {
                this.imageForm.Close();
                this.imageForm = null;
            }

            if (this.browserForm != null)
            {
                this.browserForm.Close();
                this.browserForm = null;
            }
        }


        public void Dispose()
        {
            this.HideFrame();
        }


        private void EnsureCreatedAndShowImageForm()
        {
            if (this.browserForm != null)
            {
                this.browserForm.Close();
                this.browserForm = null;
            }

            if (this.imageForm == null)
            {
                this.imageForm = new FrameDisplayServer.Browser.ImageDisplayForm();
                this.imageForm.FormClosed += new System.Windows.Forms.FormClosedEventHandler(imageForm_FormClosed);
            }

            this.imageForm.Show();
            this.imageForm.BringToFront();
        }
       

        private void EnsureCreatedAndShow() 
        {
            if (this.imageForm != null)
            {
                this.imageForm.Close();
                this.imageForm = null;
            }

            if (this.browserForm == null)
            {
                if (AppCentral.SettingsManager.BrowserType == "Mozilla")
                {
                    // TODO: Ensure xulrunner is configured.
                    this.browserForm = new FrameDisplayServer.Browser.MozillaBrowserForm();
                }
                else
                {
                    this.browserForm = new FrameDisplayServer.Browser.IEBrowserForm();
                }
                
                this.browserForm.Show();
                this.browserForm.FormClosed += new System.Windows.Forms.FormClosedEventHandler(browserForm_FormClosed);
            }

            this.browserForm.ShowWindow();
        }


        void imageForm_FormClosed(object sender, System.Windows.Forms.FormClosedEventArgs e)
        {
            this.imageForm = null;
        }

        void browserForm_FormClosed(object sender, System.Windows.Forms.FormClosedEventArgs e)
        {
            this.browserForm = null;
        }
        
    }
}
